import { WidgetOption, WidgetStyle, WidgetType, SignType, WidgetKind, RawField, WidgetData } from '@/types'
import WidgetSign from './WidgetSign.vue'
import SignSetting from './SignSetting.vue'
import { transSpecToPx } from './utils'

const INIT_SIZE = transSpecToPx(42)

const MIN_SIZE = transSpecToPx(10)

/** 印章 */
export default {
  key: SignType.SEAL,
  name: '印章',
  type: WidgetType.SEAL,
  style: WidgetStyle.SIGN,
  icon: 'fa-icon-widget-seal',

  width: INIT_SIZE,
  height: INIT_SIZE,
  minWidth: MIN_SIZE,
  minHeight: MIN_SIZE,

  /** 允许调整大小：设置模式且控件不跟随签章规格 */
  resizable: widget => !widget.isRiding && ((widget.isSet && !widget.followSignSize) || widget.signResizable),

  resizeAtCenter: true,

  component: WidgetSign,
  settingComponent: SignSetting,

  /** 签署控件使用校验 */
  usingValidate(widget) {
    // 签章控件未指定
    if (!widget.signImage) return `请指定${widget.option.name}`
  },

  clear(widget) {
    widget.sign = { id: '', img: '' }
  },

  rawWidgetCreator(widgetData, data) {
    const { followSignSize, keepSignAspect } = data

    return Object.assign(widgetData, {
      followSignSize,
      keepSignAspect
    } as WidgetData)
  },

  rawFieldGetter(widget, data) {
    const { followSignSize, keepSignAspect } = widget

    return Object.assign(data, {
      followSignSize,
      keepSignAspect
    } as RawField)
  }
} as WidgetOption
